<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>jsonp_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_net_jsonp_test.js.html">jsonp_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.net.JsonpTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.net.JsonpTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.net.Jsonp&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line22"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line23"></a>
<a name="line24"></a>// Global vars to facilitate a shared set up function.
<a name="line25"></a>
<a name="line26"></a>var timeoutWasCalled;
<a name="line27"></a>var timeoutHandler;
<a name="line28"></a>
<a name="line29"></a>var fakeUrl = &#39;http://fake-site.eek/&#39;;
<a name="line30"></a>
<a name="line31"></a>var originalTimeout;
<a name="line32"></a>function setUp() {
<a name="line33"></a>  timeoutWasCalled = false;
<a name="line34"></a>  timeoutHandler = null;
<a name="line35"></a>  originalTimeout = window.setTimeout;
<a name="line36"></a>  window.setTimeout = function(handler, time) {
<a name="line37"></a>    timeoutWasCalled = true;
<a name="line38"></a>    timeoutHandler = handler;
<a name="line39"></a>  };
<a name="line40"></a>}
<a name="line41"></a>
<a name="line42"></a>// Firefox throws a JS error when a script is not found.  We catch that here and
<a name="line43"></a>// ensure the test case doesn&#39;t fail because of it.
<a name="line44"></a>var originalOnError = window.onerror;
<a name="line45"></a>window.onerror = function(msg, url, line) {
<a name="line46"></a>  // TODO(user): Safari 3 on the farm returns an object instead of the typcial
<a name="line47"></a>  // params.  Pass through errors for safari for now.
<a name="line48"></a>  if (goog.userAgent.WEBKIT ||
<a name="line49"></a>      msg == &#39;Error loading script&#39; &amp;&amp; url.indexOf(&#39;fake-site&#39;) != -1) {
<a name="line50"></a>    return true;
<a name="line51"></a>  } else {
<a name="line52"></a>    return originalOnError &amp;&amp; originalOnError(msg, url, line);
<a name="line53"></a>  }
<a name="line54"></a>};
<a name="line55"></a>
<a name="line56"></a>function tearDown() {
<a name="line57"></a>  window.setTimeout = originalTimeout;
<a name="line58"></a>}
<a name="line59"></a>
<a name="line60"></a>// Quick function records the before-state of the DOM, and then return a
<a name="line61"></a>// a function to check that XDC isn&#39;t leaving stuff behind.
<a name="line62"></a>function newCleanupGuard() {
<a name="line63"></a>  var bodyChildCount = document.body.childNodes.length;
<a name="line64"></a>
<a name="line65"></a>  return function() {
<a name="line66"></a>    // let any timeout queues finish before we check these:
<a name="line67"></a>    window.setTimeout(function() {
<a name="line68"></a>      var propCounter = 0;
<a name="line69"></a>
<a name="line70"></a>      // All callbacks should have been deleted or be the null function.
<a name="line71"></a>      for (var id in goog.global[goog.net.Jsonp.CALLBACKS]) {
<a name="line72"></a>        if (goog.global[goog.net.Jsonp.CALLBACKS][id] != goog.nullFunction) {
<a name="line73"></a>          propCounter++;
<a name="line74"></a>        }
<a name="line75"></a>      }
<a name="line76"></a>
<a name="line77"></a>      assertEquals(
<a name="line78"></a>          &#39;script cleanup&#39;, bodyChildCount, document.body.childNodes.length);
<a name="line79"></a>      assertEquals(&#39;window jsonp array empty&#39;, 0, propCounter);
<a name="line80"></a>    }, 0);
<a name="line81"></a>  }
<a name="line82"></a>}
<a name="line83"></a>
<a name="line84"></a>function getScriptElement(result) {
<a name="line85"></a>  return result.deferred_.defaultScope_.script_;
<a name="line86"></a>}
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>// Check that send function is sane when things go well.
<a name="line90"></a>function testSend() {
<a name="line91"></a>  var replyReceived;
<a name="line92"></a>  var jsonp = new goog.net.Jsonp(fakeUrl);
<a name="line93"></a>
<a name="line94"></a>  var checkCleanup = newCleanupGuard();
<a name="line95"></a>
<a name="line96"></a>  var userCallback = function(data) {
<a name="line97"></a>    replyReceived = data;
<a name="line98"></a>  };
<a name="line99"></a>
<a name="line100"></a>  var payload = {atisket: &#39;atasket&#39;, basket: &#39;yellow&#39;};
<a name="line101"></a>  var result = jsonp.send(payload, userCallback);
<a name="line102"></a>
<a name="line103"></a>  var script = getScriptElement(result);
<a name="line104"></a>
<a name="line105"></a>  assertNotNull(&#39;script created&#39;, script);
<a name="line106"></a>  assertEquals(&#39;encoding is utf-8&#39;, &#39;UTF-8&#39;, script.charset);
<a name="line107"></a>
<a name="line108"></a>  // Check that the URL matches our payload.
<a name="line109"></a>  assertTrue(&#39;payload in url&#39;, script.src.indexOf(&#39;basket=yellow&#39;) &gt; -1);
<a name="line110"></a>  assertTrue(&#39;server url&#39;, script.src.indexOf(fakeUrl) == 0);
<a name="line111"></a>
<a name="line112"></a>  // Now, we have to track down the name of the callback function, so we can
<a name="line113"></a>  // call that to simulate a returned request + verify that the callback
<a name="line114"></a>  // function does not break if it receives a second unexpected parameter.
<a name="line115"></a>  var callbackName = /callback=([^&amp;]+)/.exec(script.src)[1];
<a name="line116"></a>  var callbackFunc = eval(callbackName);
<a name="line117"></a>  callbackFunc({some: &#39;data&#39;, another: [&#39;data&#39;, &#39;right&#39;, &#39;here&#39;]},
<a name="line118"></a>      &#39;unexpected&#39;);
<a name="line119"></a>  assertEquals(&#39;input was received&#39;, &#39;right&#39;, replyReceived.another[1]);
<a name="line120"></a>
<a name="line121"></a>  // Because the callbackFunc calls cleanUp_ and that calls setTimeout which
<a name="line122"></a>  // we have overwritten, we have to call the timeoutHandler to actually do
<a name="line123"></a>  // the cleaning.
<a name="line124"></a>  timeoutHandler();
<a name="line125"></a>
<a name="line126"></a>  checkCleanup();
<a name="line127"></a>  timeoutHandler();
<a name="line128"></a>}
<a name="line129"></a>
<a name="line130"></a>
<a name="line131"></a>// Check that send function is sane when things go well.
<a name="line132"></a>function testSendWhenCallbackHasTwoParameters() {
<a name="line133"></a>  var replyReceived, replyReceived2;
<a name="line134"></a>  var jsonp = new goog.net.Jsonp(fakeUrl);
<a name="line135"></a>
<a name="line136"></a>  var checkCleanup = newCleanupGuard();
<a name="line137"></a>
<a name="line138"></a>  var userCallback = function(data, opt_data2) {
<a name="line139"></a>    replyReceived = data;
<a name="line140"></a>    replyReceived2 = opt_data2;
<a name="line141"></a>  };
<a name="line142"></a>
<a name="line143"></a>  var payload = {atisket: &#39;atasket&#39;, basket: &#39;yellow&#39;};
<a name="line144"></a>  var result = jsonp.send(payload, userCallback);
<a name="line145"></a>  var script = getScriptElement(result);
<a name="line146"></a>
<a name="line147"></a>  // Test a callback function that receives two parameters.
<a name="line148"></a>  var callbackName = /callback=([^&amp;]+)/.exec(script.src)[1];
<a name="line149"></a>  var callbackFunc = eval(callbackName);
<a name="line150"></a>  callbackFunc(&#39;param1&#39;, {some: &#39;data&#39;, another: [&#39;data&#39;, &#39;right&#39;, &#39;here&#39;]});
<a name="line151"></a>  assertEquals(&#39;input was received&#39;, &#39;param1&#39;, replyReceived);
<a name="line152"></a>  assertEquals(&#39;second input was received&#39;, &#39;right&#39;,
<a name="line153"></a>      replyReceived2.another[1]);
<a name="line154"></a>
<a name="line155"></a>  // Because the callbackFunc calls cleanUp_ and that calls setTimeout which
<a name="line156"></a>  // we have overwritten, we have to call the timeoutHandler to actually do
<a name="line157"></a>  // the cleaning.
<a name="line158"></a>  timeoutHandler();
<a name="line159"></a>
<a name="line160"></a>  checkCleanup();
<a name="line161"></a>  timeoutHandler();
<a name="line162"></a>}
<a name="line163"></a>
<a name="line164"></a>// Check that send function works correctly when callback param value is
<a name="line165"></a>// specified.
<a name="line166"></a>function testSendWithCallbackParamValue() {
<a name="line167"></a>  var replyReceived;
<a name="line168"></a>  var jsonp = new goog.net.Jsonp(fakeUrl);
<a name="line169"></a>
<a name="line170"></a>  var checkCleanup = newCleanupGuard();
<a name="line171"></a>
<a name="line172"></a>  var userCallback = function(data) {
<a name="line173"></a>    replyReceived = data;
<a name="line174"></a>  };
<a name="line175"></a>
<a name="line176"></a>  var payload = {atisket: &#39;atasket&#39;, basket: &#39;yellow&#39;};
<a name="line177"></a>  var result = jsonp.send(payload, userCallback, undefined, &#39;dummyId&#39;);
<a name="line178"></a>
<a name="line179"></a>  var script = getScriptElement(result);
<a name="line180"></a>
<a name="line181"></a>  assertNotNull(&#39;script created&#39;, script);
<a name="line182"></a>  assertEquals(&#39;encoding is utf-8&#39;, &#39;UTF-8&#39;, script.charset);
<a name="line183"></a>
<a name="line184"></a>  // Check that the URL matches our payload.
<a name="line185"></a>  assertTrue(&#39;payload in url&#39;, script.src.indexOf(&#39;basket=yellow&#39;) &gt; -1);
<a name="line186"></a>  assertTrue(&#39;dummyId in url&#39;,
<a name="line187"></a>      script.src.indexOf(&#39;callback=_callbacks_.dummyId&#39;) &gt; -1);
<a name="line188"></a>  assertTrue(&#39;server url&#39;, script.src.indexOf(fakeUrl) == 0);
<a name="line189"></a>
<a name="line190"></a>  // Now, we simulate a returned request using the known callback function
<a name="line191"></a>  // name.
<a name="line192"></a>  var callbackFunc = _callbacks_.dummyId;
<a name="line193"></a>  callbackFunc({some: &#39;data&#39;, another: [&#39;data&#39;, &#39;right&#39;, &#39;here&#39;]});
<a name="line194"></a>  assertEquals(&#39;input was received&#39;, &#39;right&#39;, replyReceived.another[1]);
<a name="line195"></a>
<a name="line196"></a>  // Because the callbackFunc calls cleanUp_ and that calls setTimeout which
<a name="line197"></a>  // we have overwritten, we have to call the timeoutHandler to actually do
<a name="line198"></a>  // the cleaning.
<a name="line199"></a>  timeoutHandler();
<a name="line200"></a>
<a name="line201"></a>  checkCleanup();
<a name="line202"></a>  timeoutHandler();
<a name="line203"></a>}
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>// Check that the send function is sane when the thing goes south.
<a name="line207"></a>function testSendFailure() {
<a name="line208"></a>  var replyReceived = false;
<a name="line209"></a>  var errorReplyReceived = false;
<a name="line210"></a>
<a name="line211"></a>  var jsonp = new goog.net.Jsonp(fakeUrl);
<a name="line212"></a>
<a name="line213"></a>  var checkCleanup = newCleanupGuard();
<a name="line214"></a>
<a name="line215"></a>  var userCallback = function(data) {
<a name="line216"></a>    replyReceived = data;
<a name="line217"></a>  };
<a name="line218"></a>  var userErrorCallback = function(data) {
<a name="line219"></a>    errorReplyReceived = data;
<a name="line220"></a>  };
<a name="line221"></a>
<a name="line222"></a>  var payload = { justa: &#39;test&#39; };
<a name="line223"></a>
<a name="line224"></a>  jsonp.send(payload, userCallback, userErrorCallback);
<a name="line225"></a>
<a name="line226"></a>  assertTrue(&#39;timeout called&#39;, timeoutWasCalled);
<a name="line227"></a>
<a name="line228"></a>  // Now, simulate the time running out, so we go into error mode.
<a name="line229"></a>  // After jsonp.send(), the timeoutHandler now is the Jsonp.cleanUp_ function.
<a name="line230"></a>  timeoutHandler();
<a name="line231"></a>  // But that function also calls a setTimeout(), so it changes the timeout
<a name="line232"></a>  // handler once again, so to actually clean up we have to call the
<a name="line233"></a>  // timeoutHandler() once again. Fun!
<a name="line234"></a>  timeoutHandler();
<a name="line235"></a>
<a name="line236"></a>  assertFalse(&#39;standard callback not called&#39;, replyReceived);
<a name="line237"></a>
<a name="line238"></a>  // The user&#39;s error handler should be called back with the same payload
<a name="line239"></a>  // passed back to it.
<a name="line240"></a>  assertEquals(&#39;error handler called&#39;, &#39;test&#39;, errorReplyReceived.justa);
<a name="line241"></a>
<a name="line242"></a>  // Check that the relevant cleanup has occurred.
<a name="line243"></a>  checkCleanup();
<a name="line244"></a>  // Check cleanup just calls setTimeout so we have to call the handler to
<a name="line245"></a>  // actually check that the cleanup worked.
<a name="line246"></a>  timeoutHandler();
<a name="line247"></a>}
<a name="line248"></a>
<a name="line249"></a>
<a name="line250"></a>// Check that a cancel call works and cleans up after itself.
<a name="line251"></a>function testCancel() {
<a name="line252"></a>  var checkCleanup = newCleanupGuard();
<a name="line253"></a>
<a name="line254"></a>  var successCalled = false;
<a name="line255"></a>  var successCallback = function() {
<a name="line256"></a>    successCalled = true;
<a name="line257"></a>  };
<a name="line258"></a>
<a name="line259"></a>  // Send and cancel a request, then make sure it was cleaned up.
<a name="line260"></a>  var jsonp = new goog.net.Jsonp(fakeUrl);
<a name="line261"></a>  var requestObject = jsonp.send({test: &#39;foo&#39;}, successCallback);
<a name="line262"></a>  jsonp.cancel(requestObject);
<a name="line263"></a>
<a name="line264"></a>  for (var key in goog.global[goog.net.Jsonp.CALLBACKS]) {
<a name="line265"></a>    assertNotEquals(&#39;The success callback should have been removed&#39;,
<a name="line266"></a>                    goog.global[goog.net.Jsonp.CALLBACKS][key],
<a name="line267"></a>                    successCallback);
<a name="line268"></a>  }
<a name="line269"></a>
<a name="line270"></a>  // Make sure cancelling removes the script tag
<a name="line271"></a>  checkCleanup();
<a name="line272"></a>  timeoutHandler();
<a name="line273"></a>}
<a name="line274"></a>
<a name="line275"></a>function testPayloadParameters() {
<a name="line276"></a>  var checkCleanup = newCleanupGuard();
<a name="line277"></a>
<a name="line278"></a>  var jsonp = new goog.net.Jsonp(fakeUrl);
<a name="line279"></a>  var result = jsonp.send({
<a name="line280"></a>    &#39;foo&#39;: 3,
<a name="line281"></a>    &#39;bar&#39;: &#39;baz&#39;
<a name="line282"></a>  });
<a name="line283"></a>
<a name="line284"></a>  var script = getScriptElement(result);
<a name="line285"></a>  assertEquals(&#39;Payload parameters should have been added to url.&#39;,
<a name="line286"></a>               fakeUrl + &#39;?foo=3&amp;bar=baz&#39;,
<a name="line287"></a>               script.src);
<a name="line288"></a>
<a name="line289"></a>  checkCleanup();
<a name="line290"></a>  timeoutHandler();
<a name="line291"></a>}
<a name="line292"></a>
<a name="line293"></a>function testOptionalPayload() {
<a name="line294"></a>  var checkCleanup = newCleanupGuard();
<a name="line295"></a>
<a name="line296"></a>  var errorCallback = goog.testing.recordFunction();
<a name="line297"></a>
<a name="line298"></a>  var stubs = new goog.testing.PropertyReplacer();
<a name="line299"></a>  stubs.set(goog.global, &#39;setTimeout&#39;, function(errorHandler) {
<a name="line300"></a>    errorHandler();
<a name="line301"></a>  });
<a name="line302"></a>
<a name="line303"></a>  var jsonp = new goog.net.Jsonp(fakeUrl);
<a name="line304"></a>  var result = jsonp.send(null, null, errorCallback);
<a name="line305"></a>
<a name="line306"></a>  var script = getScriptElement(result);
<a name="line307"></a>  assertEquals(&#39;Parameters should not have been added to url.&#39;,
<a name="line308"></a>               fakeUrl, script.src);
<a name="line309"></a>
<a name="line310"></a>  // Clear the script hooks because we triggered the error manually.
<a name="line311"></a>  script.onload = goog.nullFunction;
<a name="line312"></a>  script.onerror = goog.nullFunction;
<a name="line313"></a>  script.onreadystatechange = goog.nullFunction;
<a name="line314"></a>
<a name="line315"></a>  var errorCallbackArguments = errorCallback.getLastCall().getArguments();
<a name="line316"></a>  assertEquals(1, errorCallbackArguments.length);
<a name="line317"></a>  assertNull(errorCallbackArguments[0]);
<a name="line318"></a>
<a name="line319"></a>  checkCleanup();
<a name="line320"></a>  stubs.reset();
<a name="line321"></a>}
</pre>


</body>
</html>
